package com.bsy.videostation.ui.activity

import android.animation.Animator
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import com.airbnb.lottie.animation.keyframe.BaseKeyframeAnimation.AnimationListener
import com.bsy.framework.base.BaseMvvmActivity
import com.bsy.videostation.viewmodel.SplashViewModel
import com.dg.videostation.databinding.ActivitySplashBinding
import com.gyf.immersionbar.ImmersionBar

class SplashActivity : BaseMvvmActivity<ActivitySplashBinding, SplashViewModel>() {

    override fun initView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this).fullScreen(true).init()
    }

    override fun initData() {
        mViewModel.loginByAccount()
    }

    override fun initListener() {
        mBinding.lottieView.addAnimatorListener(lottieListener)
    }

    override fun initObserver() {
        mViewModel.loginInfo.observe(this) {
            mBinding.lottieView.playAnimation()
        }
    }

    private val lottieListener = object : Animator.AnimatorListener {
        override fun onAnimationStart(animation: Animator) {

        }

        override fun onAnimationEnd(animation: Animator) {
            navigateToMainActivity()
        }

        override fun onAnimationCancel(animation: Animator) {

        }

        override fun onAnimationRepeat(animation: Animator) {

        }

    }

    private fun navigateToMainActivity() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }

    override fun onDestroy() {
        mBinding.lottieView.removeAnimatorListener(lottieListener)
        super.onDestroy()
    }
}
